<?php

// +----------------------------------------------
// | Name: 星空框架 单页内容管理
// +----------------------------------------------
// | Version: V5.0
// +----------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +----------------------------------------------
// | Time: 2021-05-29 11:04
// +----------------------------------------------
declare(strict_types=1);

namespace app\xingkong\controller\v1;

use app\common\common\BackendController;
use app\common\model\Artice as ArticeModel;
use think\facade\View;
use think\facade\Db;

/**
 * @xk 单页内容管理
 */
class Info extends BackendController
{
    /**
     * @xk 单页内容
     *
     * @method info
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-29T11:05:51+080
     */
    public function info(ArticeModel $articeModel)
    {
        $param = request()->param();
        $cate_id = isset($param['cate_id']) ? $param['cate_id'] : '2';
        $info = $articeModel
                ->alias('a')
                ->leftJoin('cate c', 'c.cate_id=a.cate_id')
                ->where([['a.cate_id', '=', $cate_id]])
                ->field('a.*,c.cate_name,a.`picurl` as show_picurl,`fileurl` as show_fileurl')
                ->withAttr('show_picurl',function($value,$data){
                  if($value){
                    return getShowUrl($value);
                  }
                })
                ->withAttr('show_fileurl',function($value,$data){
                  if($value){
                    return getShowUrl($value);
                  }
                })
                ->find();

        return View::fetch('', ['info' => $info ? $info->toArray() : '', 'cate_id' => $cate_id]);
    }
    /**
     * @xk 单页信息保存
     * @method  save
     * @param   ArticeModel $articeModel  [description]
     * @return  [type] [description]
     * @Version V5.0
     * @Time    2021-11-11
     * @City    青岛
     * @Author  xkyinzhe <zhonghuaxinxing@sina.cn>
     */
    public function save(ArticeModel $articeModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        $resultData = [];
        try {
            $param = request()->param();
            $act = isset($param['act']) ? $param['act'] : 'create';
            $articeModel->validate($param, $act);
            switch ($act) {
            case 'edit':
              $res = $articeModel->find($param['artice_id'])->save($param);
              break;
          }
          Db::commit();
            $result_code = 200;
            $result_msg = '操作成功';
        } catch (\Exception $e) {
          Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
}
